package com.googlecode.maratische.google.gui;

import static com.googlecode.maratische.google.MainController.getMainController;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import be.lechtitseb.google.reader.api.model.exception.GoogleReaderException;

import com.googlecode.maratische.google.BaseException;
import com.googlecode.maratische.google.DaoAction;
import com.googlecode.maratische.google.MainController;
import com.googlecode.maratische.google.MainFrame;
import com.googlecode.maratische.google.model.Command;
import com.googlecode.maratische.google.model.CommandSearchSpammedItem;
import com.googlecode.maratische.google.model.Commands;
import com.googlecode.maratische.google.model.Item;
import com.googlecode.maratische.google.model.Spam;

public class SpamForm extends JDialog {
	
	private static final long serialVersionUID = 3237971421654126882L;

	private JComboBox boxTitleType = new JComboBox(Spam.spamTypes);
	private JTextField fieldTitle = new JTextField(30);
	private JComboBox boxBodyType = new JComboBox(Spam.spamTypes);
	private JTextArea fieldBody= new JTextArea(5,30);
	private Spam spam = null;
	
	private ItemListModel spamSearchItemListModel = null;
	private JList spamSearchItems;
	
	public SpamForm(JDialog mainFrame) {
		super(mainFrame, true);
		init();
	}
	public SpamForm(JDialog mainFrame, Spam spam) {
		super(mainFrame, true);
		this.spam = spam;
		init();
	}
	public SpamForm(MainFrame mainFrame) {
		super(mainFrame, true);
		init();

	}
	private void init() {
		setSize(500, 300);
		setTitle("Spam!");
		Box box = Box.createVerticalBox();
		setLayout(new BorderLayout());
		
		
		JPanel panel1 = new JPanel();
		panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel1.add(new JLabel("Title: "));
		panel1.add(boxTitleType);
		panel1.add(fieldTitle);
		box.add(panel1);
		
		JPanel panel2 = new JPanel();
		panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel2.add(new JLabel("Body: "));
		panel2.add(boxBodyType);
		panel2.add(fieldBody);
		box.add(panel2);
		
		JPanel panel3 = new JPanel();
		panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
		JButton btnOk = new JButton("Save");
		panel3.add(btnOk);
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				save();
			}
		});
		JButton btnCancel = new JButton("Cancel");
		panel3.add(btnCancel);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (spam != null && spam.getId()>0) {
					if (JOptionPane.showConfirmDialog(SpamForm.this, "Are you sure to remove Spam Item?", "Delete spam item", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
						try {
							DaoAction.getDaoAction().getSpamDao().remove(spam.getId());
						} catch (BaseException e1) {
							getMainController().showError("Error on remove Spam Item", e1);
						}
						close();
					}
				}
			}
		});
		btnDelete.setEnabled((spam != null && spam.getId()>0) ? true : false);
		panel3.add(btnDelete);
		JButton btnTestSearch = new JButton("Test search");
		btnTestSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				testSearch();
			}
		});
		panel3.add(btnTestSearch);
		box.add(panel3);
		
		spamSearchItemListModel = new ItemListModel();
		spamSearchItems = new JList(spamSearchItemListModel);
		box.add(new JScrollPane(spamSearchItems, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
		
		add(box, BorderLayout.CENTER);

		Item item = MainController.getMainController().getSelectedItem();
		if (item != null) {
			fieldTitle.setText(item.getTitle());
		}
		if (spam != null) {
			boxTitleType.setSelectedIndex(spam.getTitleType());
			if (spam.getTitle() != null) {
				fieldTitle.setText(spam.getTitle());
			}
			boxBodyType.setSelectedIndex(spam.getDescriptionType());
			if (spam.getDescription() != null) {
				fieldBody.setText(spam.getDescription());
			}
		}
	}

	private void testSearch() {
		CommandSearchSpammedItem command = new CommandSearchSpammedItem();		
		Spam spamSearch = new Spam();
		spamSearch.setTitleType(boxTitleType.getSelectedIndex());
		spamSearch.setTitle(fieldTitle.getText());
		spamSearch.setDescriptionType(boxBodyType.getSelectedIndex());
		spamSearch.setDescription(fieldBody.getText());
		
		try {
			List<Item> items = command.findSpamItems(DaoAction.getDaoAction(), spamSearch);
			spamSearchItemListModel.clearItems();
			spamSearchItemListModel.addAll(items);
			spamSearchItems.updateUI();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void save() {
			if (spam == null) {
				spam = new Spam();
				spam.setCount(0);
			}
			spam.setTitleType(boxTitleType.getSelectedIndex());
			spam.setTitle(fieldTitle.getText());
			spam.setDescriptionType(boxBodyType.getSelectedIndex());
			spam.setDescription(fieldBody.getText());
			spam.setDateStop(new Date());
			
			if (spam.getTitle().length()+spam.getDescription().length() == 0) {
				JOptionPane.showMessageDialog(this, "Anti Spam message is empty", "Spam", JOptionPane.INFORMATION_MESSAGE);
				return;
			}

			try {
				spam = DaoAction.getDaoAction().getSpamDao().save(spam);
				CommandSearchSpammedItem command = (CommandSearchSpammedItem)Command.createCommand(Commands.SearchSpammedItem);
				command.setInteger1(spam.getId());
				command.execute(DaoAction.getDaoAction());
			} catch (SQLException e1) {
				getMainController().showError("Ошибка сохранения Spam", e1);
			} catch (BaseException e1) {
				getMainController().showError("Ошибка сохранения Spam", e1);
			} catch (GoogleReaderException e1) {
				getMainController().showError("Ошибка сохранения Spam", e1);
			}
		close();
	}
	
	private void close() {
		this.dispose();
	}
	
	private Spam getSpam() {
		return this.spam;
	}

}
